<template>
  <div class="app-container">
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
      </el-row>
    </div>

    <!--表格内容-->
    <el-table v-loading="loading" :data="menulist" @selection-change="getMenusList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜单编号" align="center" prop="menuId":show-overflow-tooltip="true" />
      <el-table-column label="菜单名称" align="center" prop="dishName" :show-overflow-tooltip="true" />
      <el-table-column label="菜单价格" align="center" prop="price" :show-overflow-tooltip="true" />
      <el-table-column label="所属店家" align="center" prop="storeName" :show-overflow-tooltip="true" />
      <el-table-column label="所属店家地址" align="center" prop="storeAddress" :show-overflow-tooltip="true" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" :show-overflow-tooltip="true" />
      <el-table-column label="更新时间" align="center" prop="lastUpdateTime" :show-overflow-tooltip="true" />
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:dict:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:dict:remove']"
                >删除</el-button>
              </template>
            </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getMenusList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜品名称" prop="dishName">
          <el-input v-model="form.dishName" placeholder="请输入菜品名称"/>
        </el-form-item>
        <el-form-item label="菜品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入菜品价格"/>
        </el-form-item>
        <el-form-item label="所属店家ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入所属店家ID"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script >
import {addMenu, listMenus} from "@/api/dish/menu";

export default {
  name: 'Menu',
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      action: "",
      open: false,
      // 遮罩层
      loading: true,
      //菜单信息
      menulist:[],
      total:0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        dishName: '',
        price: '',
        storeId:''
      },
    }
  },
  created() {
    this.getMenusList()
  },
  methods: {
    //获取菜单信息
    async getMenusList(){
      this.loading = true
      const data = await listMenus({},this.queryParams);
      this.total = data.total;
      this.menulist = data.rows;
      this.loading = false
      /*this.menulist.push({
        menuId: '1',
        dishName: '菜品名称1',
        price: '100',
        storeName: '店铺名称1',
        storeAddress: '店铺地址1',
        contactPhone: '1234567890',
      })*/
    },
    // 新增商店
    async handleAdd() {
      this.title = '新增菜品';
      this.action = 'add';
      this.open = true;
    },
    //表单重置
    reset() {
      this.form = {
        dishName: '',
        price: '',
        storeId:''
      }
    },
    // 提交表单
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.action === 'add') {
            // 新增逻辑
            const data = await addMenu(this.form);
            console.log(data);
            this.$modal.msgSuccess(data.msg);
            this.open = false
            await this.getMenusList();
          }
        }
      })
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  }
}
</script>
